package com.kb.design.mapper.postgreSQL;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kb.design.entity.CustomUser;
import com.kb.design.entity.DesignCommunicate;
import com.kb.design.entity.DesignDept;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 订单表(DesignOrder)表数据库访问层
 *
 * @author makejava
 * @since 2023-11-24 10:56:46
 */
@Mapper
@DS("slave-1")
public interface DesignDeptMapper extends BaseMapper<DesignDept> {

    //SELECT d.dept_name FROM design_user u LEFT JOIN design_dept d on u.dept_id = d.dept_id WHERE u.code = '23988'
    @Select("SELECT d.dept_name FROM design_dept d LEFT JOIN design_user u on u.dept_id = d.dept_id WHERE u.code = #{code}")
    String queryDeptNameByUserCode(String code);

    @Select("SELECT * FROM design_dept d WHERE d.leader like CONCAT('%', #{code}, '%') and d.del_flag = 0")
    List<DesignDept> queryDeptLeaderByUserCode(String code);

    @Select("SELECT * FROM design_dept WHERE ',' || ancestors || ',' LIKE CONCAT('%,', #{parentId}, ',%')")
    List<DesignDept> selectByAncestorId(@Param("parentId") Long parentId);
    //select count(1) from sys_user where dept_id = #{deptId} and del_flag = '0'
//    @Select("select count(1) from design_user where dept_id = #{deptId} and del_flag = '0'")
    public int checkDeptExistUser(Long deptId);

//    @Select("select d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.phone, d.email, d.status,(select dept_name from sys_dept where dept_id = d.parent_id) parent_name from sys_dept d where d.dept_id = #{deptId}")
    public DesignDept selectDeptById(Long deptId);

    public List<DesignDept> selectChildrenDeptById(Long deptId);

    public int updateDeptChildren(@Param("depts") List<DesignDept> depts);

    public int updateDept(DesignDept dept);

    public void updateDeptStatusNormal(Long[] deptIds);

    List<CustomUser> selectUsersByDeptAndChildren(@Param("deptId") Long deptId);

    Integer selectUsersPinByDeptAndChildren(@Param("deptId") Long deptId);
    List<DesignDept> getAllSubDeptByAncestors(@Param("deptId") Long deptId);
}

